/** 
 * https://leetcode.cn/problems/frequency-of-the-most-frequent-element/
 * 1838. 最高频元素的频数 
 * medium, 王顶成 2024.9.3
 * c++  排序+滑动窗口
 */
#include <bits/stdc++.h>
using namespace std;

class Solution {
public:
    int maxFrequency(vector<int>& nums, int k) {
        sort(nums.begin(), nums.end());
        int n = nums.size();
        long long sum = 0;
        int l = 0, res = 1;
        for (int r = 1; r < n; ++r) {
            sum += (long long)(nums[r] - nums[r - 1]) * (r - l);
            while (sum > k) {
                int temp;
                temp=nums[r] - nums[l];
                sum-=temp;
                ++l;
            }
            res = max(res, r - l + 1);
        }
        return res;
    }
};
